/**
 * Copyright 2019 Inc.
 **/
package com.myz.demo;

import feign.Feign;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.auth.BasicAuthRequestInterceptor;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;

import java.util.List;

/**
 * 使用拦截器
 *
 * @author maoyz0621 on 19-7-13
 * @version: v1.0
 */
public class ForwardedForInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        template.header("X-Forwarded-For", "www.baidu.com");
    }

    public static void main(String[] args) {
        GitHub github = Feign.builder()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                // 请求头添加信息
                .requestInterceptor(new ForwardedForInterceptor())
                .target(GitHub.class, "https://api.github.com");

        GitHub target = Feign.builder()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                // 添加认证信息
                .requestInterceptor(new BasicAuthRequestInterceptor("maoyz", "123456"))
                .target(GitHub.class, "https://api.github.com");

        List<Contributor> contributors = github.contributors("OpenFeign", "feign");
        for (Contributor contributor : contributors) {
            System.out.println(contributor.login + " (" + contributor.contributions + ")");
        }
    }
}
